/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: CookieUtil
 * Author:   zhouheng
 * Date:     2019/4/17 22:15
 * Description: cookie工具类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.portal.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 〈一句话功能简述〉<br> 
 * 〈cookie工具类〉
 *
 * @author zhouheng
 * @create 2019/4/17
 * @since 1.0.0
 */
@Slf4j
public class CookieUtil {
    /**
     * @description: 获取指定key对应的value
     * @date: 2019/4/17 22:30
     * @author:zhouheng
     */
    public  static String getCookieValue(HttpServletRequest request,String key){
        Cookie[] cookies = request.getCookies();
        if (ArrayUtils.isNotEmpty(cookies)){
            for (Cookie cookie : cookies){
                if (StringUtils.equals(key,cookie.getName())){
                    return cookie.getValue();
                }
            }
        }

        return null;
    }

    /**
     * @description: 是否事ajax请求
     * @date: 2019/4/17 22:30
     * @author:zhouheng
     */
    public static boolean isAjax(HttpServletRequest request) {
        boolean isAjaxRequest = false;
        if(!StringUtils.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")){
            isAjaxRequest = true;
        }
        return isAjaxRequest;
    }
}